package com.lnkj.spcj.net

import android.content.Intent
import com.alibaba.fastjson.JSON
import com.google.gson.Gson
import com.google.gson.JsonParseException
import com.lnkj.spcj.BuildConfig
import com.lnkj.spcj.R
import com.lnkj.spcj.base.BaseBean
import com.lnkj.spcj.base.WebViewActivity2
import com.lnkj.spcj.util.utilcode.ActivityUtils
import com.lnkj.spcj.util.utilcode.ToastUtils
import com.lnkj.spcj.util.utilcode.Utils
import com.lzy.okgo.callback.AbsCallback
import com.lzy.okgo.exception.HttpException
import com.lzy.okgo.exception.StorageException
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import org.json.JSONException
import java.lang.reflect.ParameterizedType
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.text.ParseException

/**
 * Copyright (c) 山东六牛网络科技有限公司 https://liuniukeji.com

 * @Description
 * *
 * @Author 与天同行的观测者
 * *
 * @Copyright Copyright (c) 山东六牛网络科技有限公司 保留所有版权(https://www.liuniukeji.com)
 * *
 * @Date 2017/12/11 0011 14:12
 */

abstract class OkGoCallback<T> : AbsCallback<T>() {

    override fun onStart(request: Request<T, out Request<*, *>>?) {
        super.onStart(request)
    }

    override fun onFinish() {
        super.onFinish()
    }

    override fun onError(response: Response<T>) {
        super.onError(response)
        val e = response.exception
        e?.printStackTrace()
        when {
            e is ApiException -> {
                ToastUtils.showShort(e.message)
            }
            e is HttpException -> {
                //log打印级别，决定了log显示的详细程度
                if (BuildConfig.DEBUG) {
                    var intent = Intent(Utils.getApp(), WebViewActivity2::class.java)
                    intent.putExtra("title", "错误打印")
                    intent.putExtra("content", response.rawResponse.body()?.string()?:"")
                    ActivityUtils.startActivity(intent)
                }
//                ToastUtils.showShort(R.string.exception_http)
            }
            e is JsonParseException || e is JSONException || e is ParseException -> {
                ToastUtils.showShort(R.string.exception_parse)
            }
            e is SocketTimeoutException -> {
                ToastUtils.showShort(R.string.exception_socket_time_out)
            }
            e is UnknownHostException || e is ConnectException -> {
                ToastUtils.showShort(R.string.exception_unkownhost)
            }
            e is StorageException -> {
                ToastUtils.showShort(R.string.exception_storage)
            }
            else -> {
                ToastUtils.showShort(e.message)
            }
        }
    }

    override fun onSuccess(response: Response<T>) {}

    @Throws(Throwable::class)
    override fun convertResponse(response: okhttp3.Response): T? {
        val genType = javaClass.genericSuperclass
        val params = (genType as ParameterizedType).actualTypeArguments
        val type = params[0] as? ParameterizedType ?: throw IllegalStateException("没有填写泛型参数")
        val rawType = type.rawType
        val body = response.body() ?: return null
        val mResponse = body.string()
        response.close()
        val gson = Gson()
        if (rawType !== BaseBean::class.java) {
            val data = JSON.parseObject<T>(mResponse, type)
//            val data = gson.fromJson<T>(mResponse, type)
            return data
        } else {
            val code = JSON.parseObject(mResponse).getInteger("status")
            val info = JSON.parseObject(mResponse).getString("info")
            if (code == 1 || code == 3) {
                val lazyResponse = JSON.parseObject<BaseBean<*>>(mResponse, type)
//                val lazyResponse = gson.fromJson<BaseBean<*>>(mResponse, type)
                return lazyResponse as T
            } else {
                throw ApiException(code, info)
            }
        }
    }
}